package com.example.demo.activemq.queue;

import com.example.demo.activemq.ActivemqContants;
import com.example.demo.activemq.AsynConsumerListener;
import com.example.demo.activemq.ProducerConsumer;

import javax.jms.JMSException;
import javax.jms.Session;

public class AsynQueueConsumer extends ProducerConsumer {
    public AsynQueueConsumer(String name) throws JMSException {
        this.name = name;
        //通过链接工厂获取链接
        this.connection = this.getConnection();
        //启动链接
        this.connection.start();
        //创建session
        this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建链接消息队列
        this.destination = this.session.createQueue(ActivemqContants.QUEUE_NAME);
        //创建消费者
        this.messageConsumer = this.session.createConsumer(this.destination);
        //设置消息监听
        this.messageConsumer.setMessageListener(new AsynConsumerListener("AsynConsumerListener:", this));
    }
}
